package com.gurun.codes.base;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;

import com.gurun.base.DebounceObservableTransformer;
import com.jakewharton.rxbinding2.support.v7.widget.RxToolbar;

import io.reactivex.functions.Consumer;

public abstract class BaseToolbarActivity extends BaseAppCompatActivity {

    protected void setUpToolbar(Toolbar toolbar) {
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(true);
            }

            RxToolbar.navigationClicks(toolbar)
                    .compose(new DebounceObservableTransformer<>())
                    .compose(this.bindToLifecycle())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            onBackPressed();
                        }
                    });
        }
    }

    /**
     * 后退的时候按栈回退
     */
    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStackImmediate();
        } else {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
